clear
screenRect=get(0,'ScreenSize');
screenpixW=screenRect(3);
screenpixH=screenRect(4);
c_x=screenRect(3)/2;
c_y=screenRect(4)/2;

c=define_colors(256);
c.BACKGROUND=c.BLACK;
c.GO=c.RED;
c.NG=c.RED;
c.TTEXT=c.WHITE;
edf_name='edf_name';
HideCursor;
ListenChar(2);%enable listening, additionally any output of keypresses to Matlab windows is suppressed.
%% screen
Screen('Preference','SkipSyncTests',1);
Screen ('Preference','VisualDebugLevel',0);
Screen('Preference','SuppressAllWarnings',1);
[w,windowRect]=Screen('OpenWindow',1);%for desktop
pause(.01);
Screen('fillrect',w,c.BACKGROUND);
screen('flip',w);
SCREEN('TextStyle',w,1);% 0=plain, 1=bold, 2=italics, 3=1&2,  4=underlined, 5=4&1,6=4&2, 7=4&3. rem(?,8)
%[oldFontName,oldFontNumber]=
SCREEN('TextFont',w,'Tahoma');
SCREEN('TextSize',w,40);
ttext='Hit the space bar to continue';
tsize=Screen('TextBounds', w, ttext);twidth=tsize(3);
SCREEN('DrawText',w,ttext,c_x-(twidth/2),c_y-100,c.TTEXT);
%warning off MATLAB:DeprecatedLogicalAPI
screen('flip',w);
fprintf('Hit the space bar to  continue\n')
uimenufcn(0,'WindowCommandWindow');
keyCode(32)=0;
while ~keyCode(32)% SPACE on pcwin
    [keyIsDown,secs,keyCode] = KbCheck; % Warning: mxSetLogical is deprecated and will not be supported in the future. Please refer to the Release Notes for information on updating your code
    if keyCode(27) % esc key
        Screen('closeall');
        ListenChar(0);
        return
    end
end


%%%%%%%%%%%%%%%%%%%%%%%%
%% start EL1k stuff

%fprintf('\n%5d,%5d,(%c)\n',i,j,ch(end))

%%%%%%%%%%%%%%%%%%%%%%%%
status = Eyelink('initialize');
if (status~=0)
    clc
    disp(' ');
    status = Eyelink('InitializeDummy');   
else
    openfile = Eyelink('openfile',[edf_name,'.edf']);
    if openfile~=0
        disp(['Cannot create EDF file: ' edf_name]);
        Eyelink('Shutdown');
        Screen('closeall');
        ListenChar(0);
        return;
    end
    %clear openfile
end
eyelink('command','clear_screen %d',0);
el=EyelinkInitDefaults(w);
el.backgroundcolour=c.BACKGROUND(1);
el.targetbeep=1;
%el.backgroundcolour = 1;
el.foregroundcolour = 225;
el.eyeimgsize=30;
if ~isempty(el.window) & ~isempty(el.callback) %#ok<AND2>
% Yes. Assign it to our dispatch callback:
% EyelinkDispatchCallback(el);
PsychEyelinkDispatchCallback(el);
end

%EyeLink('Command','file_event_filter = LEFT,RIGHT, FIXATION, SACCADE, BLINK, MESSAGE, INPUT');%sets which events will be written to the EDF file.
%EyeLink('Command','link_event_filter = LEFT,RIGHT, FIXATION, INPUT'); %sets which types of events will be sent through link
%EyeLink('Command','link_sample_data = LEFT,RIGHT, GAZE, AREA'); %sets data in samples sent through link

Eyelink('command', 'add_file_preamble_text', 'Recorded by BCOE');
%   SET UP TRACKER CONFIGURATION
%Eyelink('command', 'calibration_type = HV9');
Eyelink('Command','calibration_type = HV5');

%	set parser (conservative saccade thresholds)
%Eyelink('command', 'saccade_velocity_threshold = 35');
%Eyelink('command', 'saccade_acceleration_threshold = 9500');
%	set EDF file contents

Eyelink('command', 'file_event_filter = LEFT,RIGHT,SACCADE,MESSAGE,BUTTON,INPUT');
% Eyelink('command', 'file_event_filter = LEFT,RIGHT,MESSAGE');
% Sets which types of events will be written to EDF file. See tracker file
% DATA.INI for types.
% Arguments: <list>: list of event types
% LEFT, RIGHT events for one or both eyes
% FIXATION fixation start and end events
% FIXUPDATE fixation (pursuit) state updates
% SACCADE saccade start and end
% BLINK blink start an end
% MESSAGE messages (user notes in file)
% BUTTON button 1..8 press or release
% INPUT changes in input port lines

Eyelink('command', 'link_event_filter = LEFT,RIGHT,SACCADE,BLINK,MESSAGE,BUTTON');
% Sets which types of events will be sent through link. See tracker file
% DATA.INI for types.
% Arguments: <list>: list of event types
% LEFT, RIGHT events for one or both eyes
% FIXATION fixation start and end events
% FIXUPDATE fixation (pursuit) state updates
% SACCADE saccade start and end
% BLINK blink start an end
% MESSAGE messages (user notes in file)
% BUTTON button 1..8 press or release

Eyelink('command', 'link_sample_data  = LEFT,RIGHT,GAZE,PUPIL,AREA,BUTTON,INPUT');
% Sets data in samples sent through link. See tracker file DATA.INI for
% types.
% Arguments: <list>: list of data types
% GAZE Screen xy (gaze) position
% GAZERES units-per-degree Screen resolution
% HREF head-referenced gaze
% PUPIL raw eye camera pupil coordinates
% AREA pupil area
% STATUS warning and error flags
% BUTTON button state and change flags
% INPUT input port data lines

% how do i send the command window to the front?
ttext='Eye tracker set up starting...';
tsize=Screen('TextBounds', w, ttext);twidth=tsize(3);
Screen('DrawText',w,ttext,c_x-(twidth/2),c_y-100,c.TTEXT);
screen('flip',w);
fprintf('Eye tracker set up starting...\n')

%err=dotrackersetup(el,el.ENTER_KEY);
fprintf(' entered EyelinkDoTrackerSetup\n')
error = EyelinkDoTrackerSetup(el); % This calls the calibration,error: Screen( 'FillRect',  el.window, el.backgroundcolour );	% clear_cal_display()
fprintf('  exited EyelinkDoTrackerSetup\n')
%do_tracker_setup
% err=Eyelink('StartSetup')
% EyelinkDoTrackerSetup
el.targetbeep=0;
% eye_used = Eyelink('EyeAvailable?') % get eye that's tracked ONLY VALID AFTER STARTSAMPLES EVENT READ
% if eye_used == el.BINOCULAR; % if both eyes are tracked
%     eye_used = el.LEFT_EYE; % use left eye
% end
% showcursor;
% %eye_used=0%
% if eye_used==-1
%     % 	eye_used=0;
%     ListenChar(0);
%     Screen('closeall');
%     %Screen(buffer,'Close');
%     Eyelink('stoprecording');
%     Eyelink('closefile');
%     Eyelink('setofflinemode');
%     Eyelink('shutdown');
%     error('eye_used==-1');
% end
Screen('closeall');
    ListenChar(0);
%Screen(buffer,'Close');
Eyelink('stoprecording');
Eyelink('closefile');
Eyelink('setofflinemode');
Eyelink('shutdown');

